export default {
  guid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      let r = Math.random() * 16 | 0,
        v = c == 'x' ? r : (r & 0x3 | 0x8);
      return v.toString(16);
    })
  },
  formatDate(val, type) {
    if (typeof (val) == 'string') {
      val = val.replace(/-/g, '/')
    }
    let start = new Date(val)
    let y = start.getFullYear()
    let m = (start.getMonth() + 1) >= 10 ? (start.getMonth() + 1) : '0' + (start.getMonth() + 1)
    let d = start.getDate() >= 10 ? start.getDate() : '0' + start.getDate()
    let h = start.getHours()
    let mm = start.getMinutes() >= 10 ? start.getMinutes() : '0' + start.getMinutes()
    let s = start.getSeconds() >= 10 ? start.getSeconds() : '0' + start.getSeconds()
    let str = y + '-' + m + '-' + d
    if (type == 'yyyy') {
      str = y
    } else if (type == 'MM') {
      str = m
    } else if (type == 'yyyy-MM') {
      str = y + '-' + m
    } else if (type == 'MM-dd') {
      str = m + '-' + d
    } else if (type == 'yyyy-MM-dd HH:mm:ss') {
      str = y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + s
    }else if (type == 'yyyy年MM月dd日 HH:mm:ss') {
      str = y + '年' + m + '月' + d + '日 ' + h + ':' + mm + ':' + s
    } else if (type == 'HH:mm:ss') {
      str = h + ':' + mm + ':' + s
    }
    return str
  },
  getMonthTime() {//当月时间
    let sdate = this.formatDate(new Date(), 'yyyy-MM')
    let edate = ''
    if (sdate.split('-')[1] == '12') {
      edate += (parseInt(sdate.split('-')[0]) + 1) + '-01-01'
    } else {
      edate += sdate.split('-')[0] + '-' + (parseInt(sdate.split('-')[1]) + 1) + '-01'
    }
    return [sdate + '-01', edate]
  },
  //判断是否是空对象或空数组
  isEmpty(value) {
    return (Array.isArray(value) && value.length === 0) ||
      (Object.prototype.isPrototypeOf(value) && Object.keys(value).length === 0);
  },
  getYMDHMS(startTime, endTime) {
    var dateInterval = endTime.getTime() - startTime.getTime()
    //计算出相差天数
    var days = Math.floor(dateInterval / (24 * 60 * 60 * 1000))
    //计算小时数
    var hourLevel = dateInterval % (24 * 60 * 60 * 1000)
    var hours = Math.floor(hourLevel / (60 * 60 * 1000))
    //计算分钟数
    var minutesLevel = hourLevel % (60 * 60 * 1000)
    var minutes = Math.floor(minutesLevel / (60 * 1000))
    //计算秒数
    var seconds = Math.round((minutesLevel % (60 * 1000)) / 1000)
    return {days: days, hours: hours, minutes: minutes, seconds: seconds}
  },
  //封装过期控制代码
  setVal(key, value) {
    let curTime = new Date().getTime();
    sessionStorage.setItem(key, JSON.stringify({
      data: value,
      time: curTime
    }));
  },
  getVal(key, exp) {
    if (!exp) {
      exp = 1000 * 60 * 60 * 24
    }
    let data = sessionStorage.getItem(key)
    let dataObj = JSON.parse(data);
    let dataObjDatatoJson = null;
    if (dataObj && (new Date().getTime() - dataObj.time < exp)) {
      dataObjDatatoJson = dataObj.data;
    }
    return dataObjDatatoJson;
  },
  fixedCell({row, column, rowIndex, columnIndex}) {
    if (column.fixed == "right") {
      return 'fixed_right'
    }
    if (columnIndex == 0) {
      return 'fixed_left'
    }
  },
}
